package cn.bone.zk.zk04;

import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class CuratorLockThread implements Runnable{
    private InterProcessMutex lock;
    private String name;
    private Logger logger = LoggerFactory.getLogger(CuratorLockThread.class);

    public CuratorLockThread(InterProcessMutex lock, String name){
        this.lock = lock;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            lock.acquire();
            logger.info("{}, 获取到了分布式锁...1", name);

            // 锁的重入机制
            lock.acquire();
            logger.info("{}, 再次获取到了分布式锁...2", name);

            Thread.sleep(5000);

            lock.release();
            logger.info("{}, 释放了分布式锁...1", name);
            lock.release();
            logger.info("{}, 再次释放了分布式锁...2", name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}